package cn.iocoder.yudao.module.erp.controller.admin.warehouseinlist;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.warehouseinlist.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.warehouseinlist.WarehouseInListDO;
import cn.iocoder.yudao.module.erp.service.warehouseinlist.WarehouseInListService;

@Tag(name = "管理后台 - ERP 入库单表（一二级库）")
@RestController
@RequestMapping("/erp/warehouse-in-list")
@Validated
public class WarehouseInListController {

    @Resource
    private WarehouseInListService warehouseInListService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 入库单表（一二级库）")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-list:create')")
    public CommonResult<Long> createWarehouseInList(@Valid @RequestBody WarehouseInListSaveReqVO createReqVO) {
        return success(warehouseInListService.createWarehouseInList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 入库单表（一二级库）")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-list:update')")
    public CommonResult<Boolean> updateWarehouseInList(@Valid @RequestBody WarehouseInListSaveReqVO updateReqVO) {
        warehouseInListService.updateWarehouseInList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 入库单表（一二级库）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-list:delete')")
    public CommonResult<Boolean> deleteWarehouseInList(@RequestParam("id") Long id) {
        warehouseInListService.deleteWarehouseInList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 入库单表（一二级库）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-list:query')")
    public CommonResult<WarehouseInListRespVO> getWarehouseInList(@RequestParam("id") Long id) {
        WarehouseInListDO warehouseInList = warehouseInListService.getWarehouseInList(id);
        return success(BeanUtils.toBean(warehouseInList, WarehouseInListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 入库单表（一二级库）分页")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-list:query')")
    public CommonResult<PageResult<WarehouseInListRespVO>> getWarehouseInListPage(@Valid WarehouseInListPageReqVO pageReqVO) {
        PageResult<WarehouseInListDO> pageResult = warehouseInListService.getWarehouseInListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarehouseInListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 入库单表（一二级库） Excel")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-list:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarehouseInListExcel(@Valid WarehouseInListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarehouseInListDO> list = warehouseInListService.getWarehouseInListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 入库单表（一二级库）.xls", "数据", WarehouseInListRespVO.class,
                        BeanUtils.toBean(list, WarehouseInListRespVO.class));
    }

}